package com.medical.core.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  日期工具类
 */
public class DateUtil {

    public static final String DTHOURM = "HH:mm";

    /** 年月日时分秒(无下划线) yyyyMMddHHmmss */
    public static final String dtLong = "yyyyMMddHHmmss";

    /** 完整时间 yyyy-MM-dd HH:mm:ss */
    public static final String simple = "yyyy-MM-dd HH:mm:ss";

    /** 年月日(无下划线) yyyyMMdd */
    public static final String dtShort = "yyyyMMdd";

    public final static String DATEFORMATYMD = "yyyy-MM-dd";
    public final static String DATEFORMATYM = "yyyy-MM";

    public final static String DATEFORMATHMS = "yyyy-MM-dd HH:mm:ss";
    public final static String DATEFORMATHMSS = "yyyy-MM-dd HH:mm:ss:SSS";
    public final static String DATEFORMATH24MS = "yyyy-MM-dd HH24:MI:SS";
    public final static String DATEFORMATCH = "yyyy年MM月dd日";

    public final static String DATEFORMATMCH = "yyyy年MM月dd日 HH时mm分ss秒";

    public static DateFormat FORMAT_TIME = null;

    public static Calendar c = Calendar.getInstance();;
    /**
     * 默认日期格式
     */
    public static String DEFAULT_FORMAT = "yyyy-MM-dd";

    /**
     * 获取某年第一天日期
     *
     * @param year
     *            年份
     * @return Date
     */
    public static Date getCurrYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 字符串转日期格式
     *
     * @param str
     * @return 解析失败返回null
     */
    public static Date strToDate(String str) {
        return strToDate(str, DEFAULT_FORMAT);
    }

    /**
     * 字符串转日期格式
     *
     * @param str
     * @return 解析失败返回null
     */
    public static Date strToDate(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 格式化日期 date转date
     *
     * @param date
     * @return
     */
    public static Date dateToDate(Date date) {

        return dateToDate(date, DEFAULT_FORMAT);

    }

    /**
     * 格式化日期 date转date
     *
     * @param date
     * @return
     */
    public static Date dateToDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s_date = sdf.format(date);
        try {
            return sdf.parse(s_date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 格式化日期 Date转String
     *
     * @param date
     *            日期对象
     * @return String 日期字符串
     */
    public static String dateToString(Date date, String format) {
        SimpleDateFormat f = new SimpleDateFormat(format);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * 格式化日期 Date转String
     *
     * @param date
     *            日期对象
     */
    public static String dateToString(Date date) {
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * 毫秒数转换成时间字符串
     *
     * @param longDate
     * @return
     */
    public static String changTimeMillisToStr(Long longDate) {
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
        String sDate = f.format(longDate);
        return sDate;
    }

    /**
     * 毫秒数转换成时间字符串
     *
     * @param longDate
     * @return
     */
    public static String changTimeMillisToStr(Long longDate, String format) {
        SimpleDateFormat f = new SimpleDateFormat(format);
        String sDate = f.format(longDate);
        return sDate;
    }

    /**
     * 给传入的字符串时间 变成最早时间
     *
     * @param begin
     *            时间 如2014-01-01
     * @return 2014-01-01 00:00:00:
     */
    public static String convertBeginTime(String begin) {
        if (begin == null || begin.trim().equals("")) {
            return begin;
        }
        return begin.trim() + " 00:00:00";
    }

    /**
     * 给传入的字符串时间 变成最晚时间
     *
     * @param end
     *            时间 如2014-01-01
     * @return 2014-01-01 00:00:00:
     */
    public static String convertEndTime(String end) {
        if (end == null || end.trim().equals("")) {
            return end;
        }
        return end.trim() + " 23:59:59";
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year
     *            年份
     * @return Date
     */
    public static Date getCurrYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    /**
     * 获取本年最后一天日期
     *
     *            年份
     * @return Date
     */
    public static Date getCurrYearLast() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }

    /**
     * 指定月份的最后一天
     */
    public static Map<String, String> getTheLastDayOfMonth(String date) {
        Map<String, String> map = new HashMap<String, String>();
        Calendar calendar = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = "";
        if (null != date && !date.equals("")) {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM");
            try {
                calendar.setTime(sdf.parse(date));
            } catch (ParseException e) {
                throw new RuntimeException("传入的日期格式不正确");
            }
        } else {
            calendar.add(Calendar.MONTH, -1);// 上个月
        }
        calendar.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        calendar.roll(Calendar.DATE, -1);// 日期往前一天
        dateStr = df.format(calendar.getTime());

        String begin = dateStr + " 00:00:00";
        String end = dateStr + " 23:59:59";
        map.put("begin", begin);
        map.put("end", end);
        map.put("time", dateStr);
        return map;
    }

    /**
     * 判断输入的字符串是否为日期格式(2011-04-04 10:36:00)
     *
     * @param str 需要判断的字符串
     * @return 是返回true，否则返回false
     * */
    public static boolean isDateTime(String str) {
        String  reg = "^(((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d)$";
//		String regex = "^(((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d)$";
        Pattern pattern = Pattern.compile(reg);
        Matcher m = pattern.matcher(str);
        return m.matches();
    }
    /**
     * 判断是否为日期(例如2011-04-04)
     *
     * @param str 需要判断的字符串
     * @return 是返回true，否则返回false
     * */
    public static boolean isDate(String str) {
        String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-9]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(str);
        return m.matches();
    }

    /**
     * 判断是否为日期(例如10:41:12)
     *
     * @param str 需要判断的字符串
     * @return 是返回true，否则返回false
     * */
    public static boolean isTime(String str) {
        String regex = "^(20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(str);
        return m.matches();
    }

    /**
     * 返回当前日期的下一天的日期
     */
    public static String returnNext(String datestr) {
        Long begintemp = new Long(strToDate(datestr).getTime());
        begintemp = begintemp + (24 * 60 * 60 * 1000);
        return changTimeMillisToStr(begintemp);
    }

    /**
     *
     * 方法描述: 获取当前日期是周几<br>
     *
     * @param dt
     * @return
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     *
     * 方法描述: 获取日期所在州的周几<br>
     *
     * @param str
     * @return
     */
    public static String getWeekDay(String str) {
        return getWeekOfDate(strToDate(str));
    }

    /**
     * 获得本周一的日期
     */
    public static String getMondayOFWeek() {
        SimpleDateFormat df = new SimpleDateFormat(DEFAULT_FORMAT);//设置日期格式
        Calendar cld = Calendar.getInstance(Locale.CHINA);
        cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cld.setTimeInMillis(System.currentTimeMillis());//当前时间
        cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return df.format(cld.getTime());
    }

    /**
     * 获得本周星期日的日期
     */
    public static String getCurrentWeekday() {
        SimpleDateFormat df = new SimpleDateFormat(DEFAULT_FORMAT);//设置日期格式
        Calendar cld = Calendar.getInstance(Locale.CHINA);
        cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cld.setTimeInMillis(System.currentTimeMillis());//当前时间
        cld.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return df.format(cld.getTime());
    }



    /**
     * 取得指定日期所在周的第一天
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 取得指定日期所在周的最后一天
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     *
     * 方法描述: 获取开始结束中间的间隔日期<br>
     *
     * @param begin
     * @param end
     * @return
     * @throws ParseException
     */
    public static List<String> getIntervalDates(String begin, String end) {

        try {
            List<String> allDate = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dBegin = sdf.parse(begin);
            Date dEnd = sdf.parse(end);
            allDate.add(sdf.format(dBegin));
            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(dBegin);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
            // 测试此日期是否在指定日期之后
            while (dEnd.after(calBegin.getTime())) {
                // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                calBegin.add(Calendar.DAY_OF_MONTH, 1);
                allDate.add(sdf.format(calBegin.getTime()));
            }
            return allDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<Date> getWeekByTime(String begin, String end) {
        List<Date> endlist = new ArrayList<Date>();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        Calendar c_begin = new GregorianCalendar();
        Calendar c_end = new GregorianCalendar();
        try {
            Date one = sdf.parse(begin);
            c_begin.setTime(one);
            endlist.add(one);
            c_end.setTime(sdf.parse(end));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        c_end.add(Calendar.DAY_OF_YEAR, 1); // 结束日期下滚一天是为了包含最后一天
        while (c_begin.before(c_end)) {

            if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                endlist.add(new Date(c_begin.getTime().getTime()));
            }
            c_begin.add(Calendar.DAY_OF_YEAR, 1);
        }

        return endlist;
    }

    public static List<List<Date>> getMonthByTime(String begin, String end) {
        List<Date> endlist = new ArrayList<Date>();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        Calendar c_begin = new GregorianCalendar();
        Calendar c_end = new GregorianCalendar();
        Date one = null;
        Date dowm = null;
        try {
            one = sdf.parse(begin);
            c_begin.setTime(one);
            dowm = sdf.parse(end);
            c_end.setTime(dowm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c_end.add(Calendar.DAY_OF_YEAR, 1); // 结束日期下滚一天是为了包含最后一天
        while (c_begin.before(c_end)) {

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(c_begin.getTime());
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
            // 设置Calendar日期为下一个月一号
            calendar.set(Calendar.DATE, 1);
            // 设置Calendar日期减一,为本月末
            calendar.add(Calendar.DATE, -1);
            endlist.add(new Date(calendar.getTime().getTime()));
            c_begin.add(Calendar.MONTH, 1);
        }

        List<List<Date>> end_endlist = new ArrayList<List<Date>>();
        for (int yy = 0; yy < endlist.size(); yy++) {
            List<Date> addlist = new ArrayList<Date>();
            if (yy == 0) {
                addlist.add(one);
                addlist.add(endlist.get(yy));
            } else if (yy == endlist.size() - 1) {
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(endlist.get(yy));
                calendar.set(Calendar.DAY_OF_MONTH,
                        calendar.getMinimum(Calendar.DATE));
                addlist.add(calendar.getTime());
                addlist.add(dowm);
            } else {
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(endlist.get(yy));
                calendar.set(Calendar.DAY_OF_MONTH,
                        calendar.getMinimum(Calendar.DATE));
                addlist.add(calendar.getTime());
                addlist.add(endlist.get(yy));
            }
            end_endlist.add(addlist);
        }

        return end_endlist;
    }

    /**
     * 格式化字符串为日期的函数.
     *
     * @param strDate
     *            字符串.
     * @param marked
     *            标示格式 （1:yyyy-MM-dd; 2:yyyy-MM-dd HH:mm:ss; 3:yyyy年MM月dd日;
     *            4:yyyy年MM月dd日 HH时mm分ss秒 5:自定义格式） 当marked=5时，参数dateFormat为必填项
     *            默认状态为2
     * @param format
     *            转换格式如:"yyyy-MM-dd HH:mm:ss"
     * @return 字符串包含的日期,失败则返回当前日期
     */
    public static Date parseDate(String strDate, int marked, String format) {
        try {
            if (strDate == null || strDate.equals("") || strDate.equals("null")
                    || strDate.trim().length() == 0) {
                return new Date();
            }
            switch (marked) {
                case 1:
                    FORMAT_TIME = new SimpleDateFormat(DATEFORMATYMD);
                    break;
                case 2:
                    FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS);
                    break;
                case 3:
                    FORMAT_TIME = new SimpleDateFormat(DATEFORMATCH);
                    break;
                case 4:
                    FORMAT_TIME = new SimpleDateFormat(DATEFORMATMCH);
                    break;
                case 5:
                    FORMAT_TIME = new SimpleDateFormat(format);
                    break;
                default:
                    FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS);
                    break;
            }
            return FORMAT_TIME.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * 将字符串的 yyyy-mm-dd hh:mm:ss 翻译成数据库中的Long型
     *
     * @param strDate
     * @param marked
     *            标示格式 （1:yyyy-MM-dd; 2:yyyy-MM-dd HH:mm:ss; 3:yyyy年MM月dd日;
     *            4:yyyy年MM月dd日 HH时mm分ss秒 5:自定义格式） 当marked=5时，参数dateFormat为必填项
     *            默认状态为2
     * @param
     * @return
     */
    public static Long parseString2Long(String strDate, int marked,
                                        String format) {
        return new Long(parseDate(strDate, marked, format).getTime());
    }

    /**
     * 返回系统当前时间(精确到毫秒),作为一个唯一的订单编号
     * @return
     *      以yyyyMMddHHmmss为格式的当前系统时间
     */
    public  static String getOrderNum(){
        Date date=new Date();
        DateFormat df=new SimpleDateFormat(dtLong);
        return df.format(date);
    }

    /**
     * 获取系统当前日期(精确到毫秒)，格式：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public  static String getDateFormatter(){
        Date date=new Date();
        DateFormat df=new SimpleDateFormat(simple);
        return df.format(date);
    }

    /**
     * 获取系统当期年月日(精确到天)，格式：yyyyMMdd
     * @return
     */
    public static String getDate(){
        Date date=new Date();
        DateFormat df=new SimpleDateFormat(dtShort);
        return df.format(date);
    }

    /**
     * 产生随机的三位数
     * @return
     */
    public static String getThree(){
        Random rad=new Random();
        return rad.nextInt(1000)+"";
    }


    /**
     *
     * <p>
     * Description: 获取2个时间段相差分钟数
     * </p>
     *
     * @param startTime
     * @param endTime
     * @param pattern
     * @return
     */
    public static Integer getMinutes(String startTime, String endTime, String pattern) {

        SimpleDateFormat dfs = new SimpleDateFormat(pattern);
        try {
            Date startDate = dfs.parse(startTime);
            Date endDate = dfs.parse(endTime);

            long between = (endDate.getTime() - startDate.getTime()) / 1000;// 除以1000是为了转换成秒
            long min = between / 60;
            return (int) min;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }


    /**
     *
     * 方法描述: 获取时间差<br>
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getMinutes(Date startDate, Date endDate) {
        long between = (endDate.getTime() - startDate.getTime()) / 1000;// 除以1000是为了转换成秒
        return (int) (between / 60);
    }

    /**
     *
     * 方法描述: 后的秒差<br>
     * 完成时间: 2019年9月18日 下午6:05:56<br>
     * 作者: szj<br>
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getSeconds(Date startDate, Date endDate) {
        long between = (endDate.getTime() - startDate.getTime()) / 1000;// 除以1000是为了转换成秒
        return (int) (between);
    }
    /**
     *
     * <p>
     * Description: 获取直播结束的下一个时间段
     * </p>
     *
     * @param startTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @param pattern
     *            格式化
     * @param separateTime
     *            直播时长
     * @return
     */
    public static String getSeparateMinut(String startTime, String endTime, String pattern, Integer separateTime) {
        SimpleDateFormat dfs = new SimpleDateFormat(pattern);
        try {
            Date startDate = dfs.parse(startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.MINUTE, separateTime);
            Date endDate = dfs.parse(endTime);
            Calendar endcalendar = Calendar.getInstance();
            endcalendar.setTime(endDate);
            if (calendar.before(endcalendar)) {
                return dfs.format(calendar.getTime());
            } else {
                return endTime;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     *
     * <p>
     * Description: 比较2个时间段的大小
     * </p>
     *
     * @param startTime
     * @param endTime
     * @param pattern
     * @return
     */
    public static boolean compare(String startTime, String endTime, String pattern) {

        SimpleDateFormat dfs = new SimpleDateFormat(pattern);
        try {
            Date startDate = dfs.parse(startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            Date endDate = dfs.parse(endTime);
            Calendar endcalendar = Calendar.getInstance();
            endcalendar.setTime(endDate);
            if (calendar.before(endcalendar)) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 功能描述：返回小
     *
     * @param date
     *            日期
     * @return 返回小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能描述：返回分
     *
     * @param date
     *            日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 返回秒钟
     *
     * @param date
     *            Date 日期
     * @return 返回秒钟
     */
    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 功能描述：返回毫
     *
     * @param date
     *            日期
     * @return 返回毫
     */
    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    /**
     * 得到n天之后的日期
     *
     * @param days 需要增加的天数
     * @param datetime 需要增加的日期
     * @param pattern 日期格式化格式
     * @return 得到n天之后的日期
     */
    public static String getAfterDayDate(int days, String datetime, String... pattern) {
        if (pattern.length == 0) {
            pattern = new String[] {DATEFORMATYMD};
        }
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(parseDate(datetime,5,pattern[0]));
        canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat(pattern[0]);
        String dateStr = sdfd.format(date);

        return dateStr;
    }

    /**
     * 方法描述: 获取上一周时间<br>
     *
     * @return
     */
    public static String getlastWeakTime() {
        SimpleDateFormat df = new SimpleDateFormat(DATEFORMATHMS);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.WEEK_OF_YEAR, -1);
        date = calendar.getTime();
        return df.format(date);

    }

    /**获取当前时间的整点小时时间
     * @param date
     * @param format "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String getCurrHourTime(Date date, String format){
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        date = ca.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     *
     * 方法描述: 返回当前时间和目标时间差<br>
     * @param dateStr
     * @return
     */
    public static Long getSecondDiff(String dateStr) {
        Date date = strToDate(dateStr, DATEFORMATHMS);
        long timeStamp = System.currentTimeMillis();
        return (date.getTime() - timeStamp) / 1000;
    }

    /**
     *
     * 方法描述: 获取两个时间的小时差<br>
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getHours(String startDate, String endDate) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            long start = simpleFormat.parse(startDate).getTime();
            long end = simpleFormat.parse(endDate).getTime();
            return (int) ((end - start)/(1000 * 60 * 60));
        } catch(ParseException e) {
            return 0;
        }
    }

    //获取几小时后的时间
    public static String getAfterByHourTime(int ihour){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + ihour);
        SimpleDateFormat df = new SimpleDateFormat(simple);
        return df.format(calendar.getTime());
    }

    /**
     *
     * 方法描述: 与当前时间比较大小<br>
     * @param dateStr
     * @return
     */
    public static boolean compareNow(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(new Date());
        try {
            Date nowTime = sdf.parse(now);
            Date strTime = sdf.parse(dateStr);
            return strTime.getTime() > nowTime.getTime();
        } catch(Exception e) {
            return false;
        }
    }
    /**
     * 方法名称: getAfterMonthDate <br/>
     * 方法描述: 获取N个月之后的日期<br/>
     * @param month	n个月后 复数为n个月之前
     * @param datetime 当前日期
     * @param pattern 格式化
     * @return java.lang.String
     */
    public static String getAfterMonthDate(int month, Date datetime, String... pattern) {
        if (pattern.length == 0) {
            pattern = new String[] {DATEFORMATYMD};
        }
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(datetime);
        endCalendar.add(Calendar.MONTH, month);
        return dateToString(endCalendar.getTime(), pattern[0]);
    }
    /**
     *
     * 方法描述: 获取n月之前的时间<br>
     * @param month
     * @param datetime
     * @param pattern
     * @return
     */
    public static String getBeforeMonthDate(int month, Date datetime, String... pattern) {
        if (pattern.length == 0) {
            pattern = new String[] {DATEFORMATYMD};
        }
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(datetime);
        endCalendar.add(Calendar.MONTH, -month);
        return dateToString(endCalendar.getTime(), pattern[0]);
    }
    /**
     * 方法名称: getCurrMonthStartDate <br/>
     * 方法描述: 获取当前自然月的第一天日期<br/>
     * @return java.lang.String
     */
    public static String getCurrMonthStartDate() {
        SimpleDateFormat df = new SimpleDateFormat(DEFAULT_FORMAT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,0);
        return df.format(calendar.getTime());
    }
    /**
     * 方法名称: getCurrMonthEndDate <br/>
     * 方法描述: 获取当前自然月的最后日期<br/>
     * @return java.lang.String
     */
    public static String getCurrMonthEndDate() {
        SimpleDateFormat df = new SimpleDateFormat(DEFAULT_FORMAT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return df.format(calendar.getTime());
    }

    /**
     *
     * 方法描述: 秒级时间戳<br>
     * @param timestamp
     * @return
     */
    public static String timestampToDate(Long timestamp) {
        if(timestamp == null ) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMATYMD);
        return sdf.format(new Date(Long.valueOf(timestamp + "000")));
    }

}
